package com.wc.alorithm_luogu._BFS.Meteor_Shower_S;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/12 19:57
 * @description
 * https://www.luogu.com.cn/problem/P2895
 */
public class Main {
    /**
     * 思路：
     * 典型的BFS
     * 遍历比较是否比雷炸之前先到达
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310, INF = 0x3f3f3f3f;
    static int[][] g = new int[N][N], time = new int[N][N], dist = new int[N][N];
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    static int[][] q = new int[N * N][2];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < N; i++) Arrays.fill(time[i], INF);
        while (n-- > 0) {
            int x = sc.nextInt() + 1, y = sc.nextInt() + 1, t = sc.nextInt();
            time[x][y] = Math.min(time[x][y], t);
        }
        for (int i = 1; i < N - 1; i++) {
            for (int j = 1; j < N - 1; j++) {
                int minv = INF;
                for (int k = 0; k < 4; k++) {
                    int a = i + dx[k], b = j + dy[k];
                    minv = Math.min(minv, time[a][b]);
                }
                g[i][j] = Math.min(time[i][j], minv);
            }
        }
        int hh = 1, tt = 1;
        q[1][0] = 1;
        q[1][1] = 1;
        int res = -1;
        while (hh <= tt) {
            int[] t = q[hh++];
            int x = t[0], y = t[1];
            if (g[x][y] == INF) {
                res = dist[x][y];
                break;
            }
            for (int i = 0; i < 4; i++) {
                int a = x + dx[i], b = y + dy[i];
                // 越界
                if (a < 1 || b < 1) continue;
                // 已经走过了
                if (dist[a][b] != 0) continue;

                // 没走过但是在炸弹之后来了
                if (dist[x][y] + 1 >= g[a][b]) continue;
                // 上面都满足说明可以走了
                dist[a][b] = dist[x][y] + 1;
                q[++tt][0] = a;
                q[tt][1] = b;
            }
        }

        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

